package com.example.demo.zaqizabatest;

import java.util.Random;

/**
 * @ClassName ThreadLocalExample
 * @Author l42142
 * @Date 2021/3/3 15:02
 * @Description TODO
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public class ThreadLocalExample {

    public static class MyRunnable implements Runnable {

        private ThreadLocal threadLocal = new ThreadLocal();

        @Override
        public void run() {
            threadLocal.set((int) new Random().nextInt(100));
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//
//            }
            System.out.println(threadLocal.get());
        }
    }

    public static void main(String[] args) {
        MyRunnable sharedRunnableInstance = new MyRunnable();
        Thread thread1 = new Thread(sharedRunnableInstance);
        Thread thread2 = new Thread(sharedRunnableInstance);
        thread1.start();
        thread2.start();
    }

}
